package com.moorror.codegenvel.utils;

import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @Author jiyanghuang
 * @Date 2022/4/11 2:02 AM
 */
public class MConverterUtils {

    private MConverterUtils() {
    }

    /**
     * 函数转换
     *
     * @param listA
     * @param beanConverterFun
     * @param <A>
     * @param <B>
     * @return
     */
    public static <A, B> List<B> aToBList(List<A> listA, Function<A, B> beanConverterFun) {
        List<B> result = Lists.newArrayList();
        if (CollectionUtils.isEmpty(listA)) {
            return result;
        } else {
            listA.forEach((a) -> {
                B b = beanConverterFun.apply(a);
                if(null != b){
                    result.add(b);
                }
            });
            return result;
        }
    }

    /**
     * 函数转换
     *
     * @param listA
     * @param beanConverterFun
     * @param <A>
     * @param <B>
     * @return
     */
    public static <A, B> void aToBListPass(List<A> listA, Function<A, B> beanConverterFun, List<B> bList) {
        if (CollectionUtils.isEmpty(listA)) {
            return;
        } else {
            listA.forEach((a) -> {
                B b = beanConverterFun.apply(a);
                if(null != b){
                    bList.add(b);
                }
            });
            return;
        }
    }

    public static <A, B> List<B> aToBFilterList(List<A> listA, Function<A, B> beanConverterFun, Predicate<A> filter) {
        List<B> result = Lists.newArrayList();
        if (CollectionUtils.isEmpty(listA)) {
            return result;
        } else {
            return listA.stream().filter(filter).map(beanConverterFun).filter(Objects::nonNull).collect(Collectors.toList());
        }
    }



    /**
     * List转换
     *
     * @param listA
     * @param beanConverterFun
     * @param <A>
     * @param <B>
     * @return
     */
    public static <A, B> List<List<B>> aToBListList(List<List<A>> listA, Function<A, B> beanConverterFun) {
        List<List<B>> result = Lists.newArrayList();
        if (CollectionUtils.isEmpty(result)) {
            return result;
        } else {
            listA.forEach((a) -> {
                if (!CollectionUtils.isEmpty(a)) {
                    result.add(aToBList(a, beanConverterFun));
                }
            });
            return result;
        }
    }
}
